package com.lipuwater.agent.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ismartlib.ui.banner.Banner;
import com.ismartlib.ui.banner.listener.OnBannerClickListener;
import com.ismartlib.ui.view.item.LeftRightText;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseLayoutActivity;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.product.ProductDesBean;
import com.lipuwater.agent.ui.adapter.BannerLoaderImage;
import com.lipuwater.agent.ui.dialog.ProductNormsDialog;
import com.lipuwater.agent.ui.utils.MallActivityList;
import com.lipuwater.agent.ui.widget.ShadowLayout;
import com.lipuwater.agent.utils.MapToBeanUtil;
import com.lipuwater.agent.utils.onclick.NoDoubleClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 购买产品详情
 */
public class ProductDetailsActivity extends BaseLayoutActivity implements ProductNormsDialog.Callback {

    @Bind(R.id.layout_norms)
    LeftRightText layoutNorms;
    @Bind(R.id.layout_open)
    ShadowLayout layoutOpen;
    @Bind(R.id.activity_product_details)
    RelativeLayout activityProductDetails;
    @Bind(R.id.image_banner)
    Banner imageBanner;
    @Bind(R.id.text_content)
    TextView textContent;
    @Bind(R.id.text_price)
    TextView textPrice;

    /**
     * Dialog
     */
    private ProductNormsDialog mProductNormsDialog;
    private List<String> imgList;
    private int productId;
    private String normsid = "";  //规格
    private String normsidName = "";//规格名称
    private String leaseid = "";  //租期id
    private String deposit = "";  //价格
    private NoDoubleClick mNoDoubleClick = new NoDoubleClick();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MallActivityList.getInstance().addActivity(this);
        setMyContentView(R.layout.activity_buy_product_details);
        initTitle(R.drawable.icon_aleft, "产品详情");
        viewToolbarShadow.setVisibility(View.VISIBLE);
        ButterKnife.bind(this);
        showLoading();
        getProductDesApi();
    }

    @OnClick({R.id.layout_norms, R.id.btn_open_account})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_norms:
                if (mNoDoubleClick.check()) return;
                if (mProductNormsDialog != null) {
                    mProductNormsDialog.show();
                }
                break;
            case R.id.btn_open_account:
                if (mNoDoubleClick.check()) return;
                if (normsid.equals("") || leaseid.equals("")) {
                    if (mProductNormsDialog != null) {
                        mProductNormsDialog.show();
                    }
                    return;
                }
                toOpenOrder();
                break;
        }
    }

    private void getProductDesApi() {
        productId = getIntent().getIntExtra("productId", 0);
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("producId", productId + "");
        ApiRequestsBasis.getProductDes(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                ProductDesBean productDes = (ProductDesBean) MapToBeanUtil.toJavaBean(result.getParams(), ProductDesBean.class);
                if (productDes != null) {
                    initView(productDes);
                    hideEmptyLayout();
                    return;
                }
                showNoNetWork();
            }

            @Override
            public void onError(Call call, Exception e) {
                showNoNetWork();
            }
        });
    }


    private void initView(ProductDesBean productDes) {
        imgList = new ArrayList<>();
        for (int i = 0; i < productDes.getImgList().size(); i++) {
            imgList.add(productDes.getImgList().get(i));
        }
        imageBanner.setImages(imgList)
                .setImageLoader(new BannerLoaderImage())
                .setOnBannerClickListener(new OnBannerClickListener() {
                    @Override
                    public void OnBannerClick(int position) {

                    }
                }).start();
        textContent.setText(productDes.getContent());
        if (productDes.getNewPirce() != null) {
            String[] price = productDes.getNewPirce().split("\\|");
            if (price != null && price.length > 1) {
                textPrice.setText("¥" + price[0] + "-" + price[1]);
            }
        }
        if (productDes.getProductRules() != null) {
            mProductNormsDialog = new ProductNormsDialog(this, productDes.getProductRules(), this);
        }
    }

    @Override
    public void resultsNorms(String normsid, String normsidName) {
        //规格id
        this.normsid = normsid;
        this.normsidName = normsidName;
    }

    @Override
    public void resultsLease(String leaseid, String deposit, boolean jump) {
        //租期id
        this.leaseid = leaseid;
        this.deposit = deposit;
        if (jump) {
            toOpenOrder();
        }
    }

    private void toOpenOrder() {
        Intent intent = new Intent(this, ProduceOrderActivity.class);
        intent.putExtra("normsid", normsid);
        intent.putExtra("leaseid", leaseid);
        intent.putExtra("deposit", deposit);
        intent.putExtra("productId", productId);
        if (imgList != null && imgList.size() > 0) {
            intent.putExtra("imgUrl", imgList.get(0));
        }
        intent.putExtra("deviceName", textContent.getText().toString());
        intent.putExtra("normsidName", normsidName);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MallActivityList.getInstance().removeActivity(this);
        ButterKnife.unbind(this);
        ApiRequestsBasis.cancelRequests(this);
        if (mProductNormsDialog != null) {
            mProductNormsDialog.unCallback();
        }
    }
}
